package org.jeecg.modules.mq.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.mq.constant.RabbitConstants;
import org.jeecg.modules.order.vo.MtRepairOrderPage;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@RabbitListener(queues = RabbitConstants.ORDER_QUEUE, containerFactory = "singleListenerContainerManual")
@Slf4j
public class OrderConsumer {


    @RabbitHandler
    public void process(Map orderMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) Long tag) throws IOException {
        try {
            log.info("正常队列-消费者收到消息  : {}", orderMap.get("messageData").toString());
            MtRepairOrderPage mtRepairOrderPage = (MtRepairOrderPage)orderMap.get("messageData");

//            CanteenOrderPage canteenOrderPage = (CanteenOrderPage) orderMap.get("messageData");
//            boolean flag = canteenOrderService.saveOrder(canteenOrderPage);
            boolean flag =true;
            //int i = 3/0;
            //执行完业务逻辑后，手动进行确认消费，其中第一个参数为：消息的分发标识(全局唯一);第二个参数：是否允许批量确认消费
            if (flag)
                channel.basicAck(tag, false);
            else
                channel.basicReject(tag, false);
        } catch (Exception e) {
            //第二个参数reueue重新归入队列,true的话会重新归入队列,需要人为地处理此次异常消息,重新归入队列也会继续异常
            channel.basicReject(tag, false);
        }
    }
}
